package org.project.choujiang.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.project.choujiang.mapper.CommentRepository;
import org.project.choujiang.mapper.GoodRepository;
import org.project.choujiang.mapper.UserRepository;
import org.project.choujiang.pojo.domain.Comment;
import org.project.choujiang.pojo.domain.Good;
import org.project.choujiang.pojo.domain.User;
import org.project.choujiang.pojo.dto.CommentDto;
import org.project.choujiang.pojo.ro.ClickGoodRo;
import org.project.choujiang.pojo.ro.R;
import org.project.choujiang.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 社交
 */
@RequestMapping(value ="social")
@RestController
public class SocialController {
    @Autowired
    private GoodRepository goodRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 点赞
     * @return
     */
    @PostMapping(value ="clickGood")
    public R<String> clickGood(@RequestBody ClickGoodRo clickGood){
        User loginUserInfo = TokenUtils.getLoginUserInfo();
        List<Good> allByResourceIdAndUAndUserId = goodRepository.findAllByResourceIdAndUserId(clickGood.getResourceId(), loginUserInfo.getId());
        if (clickGood.getGoodFlag() == 1){
            if (CollectionUtil.isEmpty(allByResourceIdAndUAndUserId)){
                Good good = new Good();
                good.setResourceId(clickGood.getResourceId());
                good.setUserId(loginUserInfo.getId());
                goodRepository.save(good);
            }
        }else {
            goodRepository.removeAllByResourceIdAndUserId(clickGood.getResourceId(), loginUserInfo.getId());
        }

        return R.success(null);
    }

    /**
     * 发表评论
     * @return
     */
    @PostMapping(value ="publishComment")
    public R<String> publishComment(@RequestBody Comment comment){
        User loginUserInfo = TokenUtils.getLoginUserInfo();
        comment.setCreateDate(new Date());
        comment.setUserId(loginUserInfo.getId());
        commentRepository.save(comment);
        return R.success(null);
    }

    /**
     * 获取评论列表
     * @return
     */
    @PostMapping(value ="findCommentList/{resourceId}")
    public R<List<CommentDto>> findCommentList(@PathVariable("resourceId") String resourceId){
        List<CommentDto> res = new ArrayList<>();
        Set<String> userIds = new HashSet<>();
        List<Comment> allByResourceId = commentRepository.findAllByResourceIdOrderByCreateDateDesc(resourceId);
        allByResourceId.stream()
                .forEach(ele->{
                    CommentDto son = new CommentDto();
                    res.add(son);
                    son.setContent(ele.getContent());
                    son.setUserId(ele.getUserId());
                    userIds.add(ele.getUserId());
                });

        Map<String, User> userMap = userRepository.findByIdIsIn(userIds)
                .stream()
                .collect(Collectors.toMap(ele -> ele.getId(), ele -> ele));
        res.forEach(ele->{
            User user = userMap.get(ele.getUserId());
            if (ObjectUtil.isNotNull(user)){
                ele.setHeadImgUrl(user.getHeadImgUrl());
                ele.setNickname(user.getNickname());
            }
        });

        return R.success(res);
    }
}
